/**
 * Created by mengwei666 on 2016/10/20.
 */
import javax.imageio.*;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;
import java.io.File;
class CutImage{
    public void cut(String srcPath,String destPath,double WidthScaleFrom,double HeightScaleFrom,double WidthScaleTo,double HeightScaleTo)throws IOException{
        File input = new File(srcPath);
        Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
        ImageReader reader = (ImageReader) readers.next();
        ImageInputStream image = ImageIO.createImageInputStream(input);
        reader.setInput(image, true);
        ImageReadParam param = reader.getDefaultReadParam();
        int imageIndex=0;
        int width = reader.getWidth(imageIndex);
        int height = reader.getHeight(imageIndex);
        Rectangle rect = new Rectangle((int)(width*WidthScaleFrom),(int)(height*HeightScaleFrom),(int)(width*WidthScaleTo),(int)(height*HeightScaleTo));
        param.setSourceRegion(rect);
        BufferedImage bi = reader.read(imageIndex, param);
        Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
        ImageWriter writer = (ImageWriter) writers.next();
        File output = new File(destPath);
        ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output);
        writer.setOutput(imageOutput);
        writer.write(bi);
    }
}
class EnlargeImage{
    private double [][][] toThreeDeminsion(int[] imageArray,int imgRow,int imgCol){
        double [][][] tempData=new double[imgRow][imgCol][4];
        for(int row=0;row<imgRow;row++){
            int[] aRow=new int[imgCol];  //一横行的像素点
            for(int col=0;col<imgCol;col++){
                int element=row*imgCol+col;
                aRow[col]=imageArray[element];
            }
            for(int col=0;col<imgCol;col++){
                tempData[row][col][0] = (aRow[col] >> 24) & 0xFF; // alpha
                tempData[row][col][1] = (aRow[col] >> 16) & 0xFF; // red
                tempData[row][col][2] = (aRow[col] >> 8) & 0xFF;  // green
                tempData[row][col][3] = (aRow[col]) & 0xFF;       // blue
            }
        }
        return tempData;
    }
    private int getClip(int x, int max, int min) {
        return x > max ? max : x < min ? min : x;
    }
    public void enlarge (String srcPath,String destPath,int destW,int destH)throws IOException{
        BufferedImage srcImage=ImageIO.read(new File(srcPath));
        int width=srcImage.getWidth();
        int height=srcImage.getHeight();
        int imageArray[]=new int[width*height];
        BufferedImage newImage=new BufferedImage(destW,destH,BufferedImage.TYPE_INT_RGB);
        srcImage.getRGB(0,0,width,height,imageArray,0,width);
        double[][][]  input3DData=toThreeDeminsion(imageArray,height,width);
        int[][][] dest3DData=new int[destH][destW][4];
        double colRatio=(double)width/(double)destW;
        double rowRatio=(double)height/(double)destH;
        for(int row = 0; row < destH; row++) {

            double srcRow = ((float)row) * rowRatio;
            double j = Math.floor(srcRow);
            double t = srcRow - j;

            for(int col = 0; col < destW; col++) {

                double srcCol = ((float)col) * colRatio;
                double k = Math.floor(srcCol);
                double u = srcCol - k;

                double f1 = (1.0d - t) * (1.0d - u);
                double f2 = (t) * (1.0d - u);
                double f3 = t * u;
                double f4 = (1.0d - t) * u;
                for (int i = 0; i < 4; i++) {
                    //将三维数组的像素RGBA通过二进制按位或 双线性插值到一维数组中
                    dest3DData[row][col][i] = (int)(
                            f1 * input3DData[getClip((int)j, height - 1, 0)][getClip((int)k, width - 1, 0)][i] +
                                    f2 * input3DData[getClip((int)(j + 1), height - 1, 0)][getClip((int)k, width - 1, 0)][i] +
                                    f3 * input3DData[getClip((int)(j + 1), height - 1, 0)][getClip((int)(k + 1),width - 1, 0)][i] +
                                    f4 * input3DData[getClip((int)j, height - 1, 0)][getClip((int)(k + 1), width - 1, 0)][i]
                    );
                }
            }
        }
        int[] oneDPix = new int[destH * destW * 4];

        for (int row = 0, cnt = 0; row < destH; row++) {
            for (int col = 0; col < destW; col++) {

                oneDPix[cnt] =
                        ((dest3DData[row][col][0] << 24) & 0xFF000000)
                                | ((dest3DData[row][col][1] << 16) & 0x00FF0000)
                                | ((dest3DData[row][col][2] << 8) & 0x0000FF00)
                                | ((dest3DData[row][col][3]) & 0x000000FF);

                cnt++;
            }
        }
        newImage.setRGB(0,0,destW,destH,oneDPix,0,destW);
        File outFile=new File(destPath);
        ImageIO.write(newImage,"jpg",outFile);
    }
}
public class Main {

    public static void main(String[] args) throws IOException {
        //切割图片
        CutImage cut=new CutImage();
        cut.cut("src/origin.jpg","src/AfterCut.jpg",0,0,0.5,0.5);

        //放大图片
        EnlargeImage enlarge=new EnlargeImage();
        enlarge.enlarge("src/origin.jpg","src/enlarge.jpg",840,530);

        //缩小图片
        enlarge.enlarge("src/origin.jpg","src/shrink.jpg",280,210);
    }
}
